package com.smile.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.smile.api.FlyApi;
import com.smile.api.OperateLogApi;
import com.smile.api.PayloadApi;
import com.smile.mapper.*;
import com.smile.model.dto.request.*;
import com.smile.model.domain.FeisuanAirport;
import com.smile.model.domain.FeisuanAirportDrone;
import com.smile.model.domain.FeisuanDrone;
import com.smile.model.domain.FeisuanDronePayload;
//import com.smile.model.dto.request.payload.CameraFocalLengthSetRequestDTO;
//import com.smile.model.dto.request.payload.GimbalResetRequestDTO;
//import com.smile.model.dto.request.payload.PayloadAuthorityGrabRequestDTO;
import com.smile.model.request.CameraFocalLengthSetRequest;
import com.smile.model.response.response.PayloadDataResponseDTO;
import com.smile.service.PayloadService;
import com.smile.service.task.FlyServicesExecutor;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.concurrent.locks.ReentrantLock;

@Service
@Slf4j
public class PayloadServiceImpl implements PayloadService {


    @Autowired
    private AirportMapper airportMapper;

    @Autowired
    private AirportInfoMapper  airportInfoMapper;
    @Autowired
    private OperateLogApi operateLogApi;
    @Autowired
    private DroneMapper droneMapper;

    @Autowired
    private DronePayloadMapper dronePayloadMapper;

    @Autowired
    private AirportDroneMapper airportDroneMapper;

    @Autowired
    private PayloadApi payloadApi;

    private ReentrantLock reentrantLock = new ReentrantLock();
    @Autowired
    FlyApi flyApi;

//    @Override
//    public GetFlyResultResponseDTO payloadAuthorityGrab(PayloadAuthorityGrabRequestDTO flyRequestDTO) throws IOException{
//        GetFlyResultResponseDTO getFlyResultResponseDTO = new  GetFlyResultResponseDTO();
//        //查询出相关配置
//        getFlyResultResponseDTO.setCode(200);
//        getFlyResultResponseDTO.setStatus(false);
//        FeisuanAirport feisuanAirport = airportMapper.selectById(flyRequestDTO.getId());
//        if(!ObjectUtil.isNull(feisuanAirport)){
//            QueryWrapper<FeisuanAirportDrone> queryWrapper = new QueryWrapper<>();
//            queryWrapper.eq("airport_id",feisuanAirport.getId());
//            FeisuanAirportDrone feisuanAirportDrone  = airportDroneMapper.selectOne(queryWrapper);
//            if(ObjectUtil.isNotNull(feisuanAirportDrone)){
//                FeisuanDrone feisuanDrone = droneMapper.selectById(feisuanAirportDrone.getDroneId());
//                if(ObjectUtil.isNotNull(feisuanDrone)){
//                    QueryWrapper<FeisuanDronePayload> queryWrapper1 = new QueryWrapper<>();
//                    queryWrapper1.eq("drone_sn",feisuanDrone.getDroneSn());
//                    FeisuanDronePayload feisuanDronePayload = dronePayloadMapper.selectOne(queryWrapper1);
//                    if(ObjectUtil.isNotNull(feisuanDronePayload)){
//                        PayloadAuthorityGrabRequest payloadAuthorityGrabRequest = new PayloadAuthorityGrabRequest();
//                        try {
//                            payloadAuthorityGrabRequest.setPayloadIndex(feisuanDronePayload.getPayloadIndex());
//                            payloadAuthorityGrabRequest.setGateway(feisuanAirport.getGateway());
//                            payloadApi.payloadAuthorityGrab(payloadAuthorityGrabRequest);
//                            getFlyResultResponseDTO.setStatus(true);
//                        }catch (Exception e){
//
//                        }
//                    }
//                }
//            }
//        }
//        return   getFlyResultResponseDTO;
//    }

//    @Override
//    public GetFlyResultResponseDTO cameraFocalLengthSet(CameraFocalLengthSetRequestDTO flyRequestDTO) throws IOException{
//        GetFlyResultResponseDTO getFlyResultResponseDTO = new  GetFlyResultResponseDTO();
//        //查询出相关配置
//        getFlyResultResponseDTO.setCode(200);
//        getFlyResultResponseDTO.setStatus(false);
//        QueryWrapper<FeisuanAirport>  queryWrapper3 = new QueryWrapper<>();
//        queryWrapper3.eq("gateway_sn",flyRequestDTO.getGateway());
//        queryWrapper3.last("limit 1");
//        FeisuanAirport feisuanAirport = airportMapper.selectOne(queryWrapper3);
//
//        if(!ObjectUtil.isNull(feisuanAirport)){
//            QueryWrapper<FeisuanAirportDrone> queryWrapper = new QueryWrapper<>();
//            queryWrapper.eq("airport_id",feisuanAirport.getId());
//            FeisuanAirportDrone feisuanAirportDrone  = airportDroneMapper.selectOne(queryWrapper);
//            if(ObjectUtil.isNotNull(feisuanAirportDrone)){
//                FeisuanDrone feisuanDrone = droneMapper.selectById(feisuanAirportDrone.getDroneId());
//                if(ObjectUtil.isNotNull(feisuanDrone)){
//                    QueryWrapper<FeisuanDronePayload> queryWrapper1 = new QueryWrapper<>();
//                    queryWrapper1.eq("drone_sn",feisuanDrone.getDroneSn());
//                    FeisuanDronePayload feisuanDronePayload = dronePayloadMapper.selectOne(queryWrapper1);
//                    if(ObjectUtil.isNotNull(feisuanDronePayload)){
//                        CameraFocalLengthSetRequest cameraFocalLengthSetRequest = new CameraFocalLengthSetRequest();
//                        try {
//                            cameraFocalLengthSetRequest.setPayloadIndex(feisuanDronePayload.getPayloadIndex());
//                            cameraFocalLengthSetRequest.setGateway(feisuanAirport.getGateway());
//                            cameraFocalLengthSetRequest.setCameraType(flyRequestDTO.getCameraType());
//                            cameraFocalLengthSetRequest.setZoomFactor(flyRequestDTO.getZoomFactor());
//                            payloadApi.cameraFocalLengthSet(cameraFocalLengthSetRequest);
//                            getFlyResultResponseDTO.setStatus(true);
//                        }catch (Exception e){
//
//                        }
//                    }
//                }
//            }
//        }
//        return   getFlyResultResponseDTO;
//    }


//    @Override
//    public GetFlyResultResponseDTO gimbalReset(GimbalResetRequestDTO flyRequestDTO) throws IOException{
//        GetFlyResultResponseDTO getFlyResultResponseDTO = new  GetFlyResultResponseDTO();
//        //查询出相关配置
//        getFlyResultResponseDTO.setCode(200);
//        getFlyResultResponseDTO.setStatus(false);
//        QueryWrapper<FeisuanAirport>  queryWrapper3 = new QueryWrapper<>();
//        queryWrapper3.eq("gateway_sn",flyRequestDTO.getGateway());
//        queryWrapper3.last("limit 1");
//        FeisuanAirport feisuanAirport = airportMapper.selectOne(queryWrapper3);
//        if(!ObjectUtil.isNull(feisuanAirport)){
//            QueryWrapper<FeisuanAirportDrone> queryWrapper = new QueryWrapper<>();
//            queryWrapper.eq("airport_id",feisuanAirport.getId());
//            FeisuanAirportDrone feisuanAirportDrone  = airportDroneMapper.selectOne(queryWrapper);
//            System.out.println(feisuanAirportDrone);
//            if(ObjectUtil.isNotNull(feisuanAirportDrone)){
//                FeisuanDrone feisuanDrone = droneMapper.selectById(feisuanAirportDrone.getDroneId());
//                System.out.println(feisuanDrone);
//                if(ObjectUtil.isNotNull(feisuanDrone)){
//                    QueryWrapper<FeisuanDronePayload> queryWrapper1 = new QueryWrapper<>();
//                    queryWrapper1.eq("drone_sn",feisuanDrone.getDroneSn());
//                    System.out.println(feisuanDrone.getDroneSn());
//                    FeisuanDronePayload feisuanDronePayload = dronePayloadMapper.selectOne(queryWrapper1);
//                    System.out.println(queryWrapper1);
//                    if(ObjectUtil.isNotNull(feisuanDronePayload)){
//                        GimbalResetRequest gimbalResetRequest = new GimbalResetRequest();
//                        try {
//                            gimbalResetRequest.setPayloadIndex(feisuanDronePayload.getPayloadIndex());
//                            gimbalResetRequest.setGateway(feisuanAirport.getGateway());
//                            gimbalResetRequest.setResetMode(flyRequestDTO.getResetMode());
//                            System.out.println(gimbalResetRequest);
//                            R r= payloadApi.gimbalReset(gimbalResetRequest);
//                            System.out.println("执行结果");
//                            System.out.println(r);
//                            getFlyResultResponseDTO.setStatus(true);
//                        }catch (Exception e){
//
//                        }
//                    }
//                }
//            }
//        }
//        return   getFlyResultResponseDTO;
//    }

    @Override
    public String getPayloadIndex(String gatewaySn) throws IOException{
        String payloadIndex = null;
        QueryWrapper<FeisuanAirport>  queryWrapper3 = new QueryWrapper<>();
        queryWrapper3.eq("gateway_sn",gatewaySn);
        queryWrapper3.last("limit 1");
        FeisuanAirport feisuanAirport = airportMapper.selectOne(queryWrapper3);

        if(!ObjectUtil.isNull(feisuanAirport)){
            QueryWrapper<FeisuanAirportDrone> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("airport_id",feisuanAirport.getId());
            FeisuanAirportDrone feisuanAirportDrone  = airportDroneMapper.selectOne(queryWrapper);
            if(ObjectUtil.isNotNull(feisuanAirportDrone)){
                FeisuanDrone feisuanDrone = droneMapper.selectById(feisuanAirportDrone.getDroneId());
                if(ObjectUtil.isNotNull(feisuanDrone)){
                    QueryWrapper<FeisuanDronePayload> queryWrapper1 = new QueryWrapper<>();
                    queryWrapper1.eq("drone_sn",feisuanDrone.getDroneSn());
                    FeisuanDronePayload feisuanDronePayload = dronePayloadMapper.selectOne(queryWrapper1);
                    if(ObjectUtil.isNotNull(feisuanDronePayload)){
                        CameraFocalLengthSetRequest cameraFocalLengthSetRequest = new CameraFocalLengthSetRequest();
                        try {
                            payloadIndex = feisuanDronePayload.getPayloadIndex();
                        }catch (Exception e){

                        }
                    }
                }
            }
        }
        return   payloadIndex;
    }


    @Override
    public PayloadDataResponseDTO getPayloadData(String gatewaySn) throws IOException{
        String payloadIndex = null;
        PayloadDataResponseDTO  payloadDataResponseDTO = new  PayloadDataResponseDTO();
        QueryWrapper<FeisuanAirport>  queryWrapper3 = new QueryWrapper<>();
        queryWrapper3.eq("gateway_sn",gatewaySn);
        queryWrapper3.last("limit 1");
        FeisuanAirport feisuanAirport = airportMapper.selectOne(queryWrapper3);

        if(!ObjectUtil.isNull(feisuanAirport)){
            QueryWrapper<FeisuanAirportDrone> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("airport_id",feisuanAirport.getId());
            queryWrapper.last("limit 1");
            FeisuanAirportDrone feisuanAirportDrone  = airportDroneMapper.selectOne(queryWrapper);
            if(ObjectUtil.isNotNull(feisuanAirportDrone)){
                FeisuanDrone feisuanDrone = droneMapper.selectById(feisuanAirportDrone.getDroneId());
                if(ObjectUtil.isNotNull(feisuanDrone)){
                    QueryWrapper<FeisuanDronePayload> queryWrapper1 = new QueryWrapper<>();

                    queryWrapper1.eq("parent_device_model_key",feisuanDrone.getDeviceModelKey());
                    queryWrapper1.eq("is_main_gimbal",1);
                    queryWrapper1.last("limit 1");
                    FeisuanDronePayload feisuanDronePayload = dronePayloadMapper.selectOne(queryWrapper1);
//                    System.out.println(feisuanDrone.getDeviceModelKey());
//                    System.out.println(feisuanDronePayload);
                    //主云台负载设备
                    if(ObjectUtil.isNotNull(feisuanDronePayload)){
                            payloadIndex = feisuanDronePayload.getDeviceModelKey();
                            payloadDataResponseDTO.setSn(feisuanDrone.getDroneSn());
                            payloadDataResponseDTO.setPayloadIndex(payloadIndex);
                            payloadDataResponseDTO.setGateway(feisuanAirport.getGatewaySn());
                            payloadDataResponseDTO.setAirportId(feisuanAirport.getId());
                            payloadDataResponseDTO.setDroneId(feisuanAirportDrone.getDroneId());
                            payloadDataResponseDTO.setOrganizationId(feisuanAirport.getOrganizationId());
                    }
                    QueryWrapper<FeisuanDronePayload> queryWrapper2 = new QueryWrapper<>();
                    queryWrapper2.eq("parent_device_model_key",feisuanDrone.getDeviceModelKey());
                    queryWrapper2
                            .eq("is_main_gimbal",0);
                    queryWrapper2.last("limit 1");
                    FeisuanDronePayload feisuanDronePayload2 = dronePayloadMapper.selectOne(queryWrapper2);
                    if(ObjectUtil.isNotNull(feisuanDronePayload2)){
                        payloadDataResponseDTO.setDroneAssistancePayloadIndex(feisuanDronePayload2.getDeviceModelKey());
                    }
                }
            }
        }
        return   payloadDataResponseDTO;
    }
    @Override
    public void gimbalReset(com.smile.model.dto.request.GimbalResetRequestDTO gimbalResetRequestDTO) throws MqttException {
        FlyServicesExecutor.create()
                .gimbalReset(gimbalResetRequestDTO.getResetMode(), gimbalResetRequestDTO.getPayloadIndex())
                .gateway(gimbalResetRequestDTO.getGateway())
                .execute();
    }
    @Override
    public void payloadAuthorityGrab(com.smile.model.dto.request.PayloadAuthorityGrabRequestDTO payloadAuthorityGrabRequestDTO) throws MqttException {
        FlyServicesExecutor.create()
                .payloadAuthorityGrab(payloadAuthorityGrabRequestDTO.getPayloadIndex())
                .gateway(payloadAuthorityGrabRequestDTO.getGateway())
                .execute();
    }

    @Override
    public void cameraFocalLengthSet(com.smile.model.dto.request.CameraFocalLengthSetRequestDTO cameraFocalLengthSetRequestDTO) throws MqttException {
        FlyServicesExecutor.create()
                .CameraFocalLengthSet(cameraFocalLengthSetRequestDTO.getCameraType(), cameraFocalLengthSetRequestDTO.getZoomFactor(), cameraFocalLengthSetRequestDTO.getPayloadIndex())
                .gateway(cameraFocalLengthSetRequestDTO.getGateway())
                .execute();
    }
//    @Override
//    public void payloadAuthorityGrab(PayloadAuthorityGrabRequestDTO payloadAuthorityGrabRequestDTO) throws MqttException {
//        FlyServicesExecutor.create()
//                .payloadAuthorityGrab(payloadAuthorityGrabRequestDTO.getPayloadIndex())
//                .gateway(payloadAuthorityGrabRequestDTO.getGateway())
//                .execute();
//    }

    @Override
    public void cameraPhotoTake(CameraPhotoTakeRequestDTO cameraPhotoTakeRequestDTO) throws MqttException {
        FlyServicesExecutor.create()
                .cameraPhotoTake(cameraPhotoTakeRequestDTO.getPayloadIndex())
                .gateway(cameraPhotoTakeRequestDTO.getGateway())
                .execute();
    }

    @Override
    public void cameraPhotoStop(CameraPhotoStopRequestDTO cameraPhotoStopRequestDTO) throws MqttException {
        FlyServicesExecutor.create()
                .cameraPhotoStop(cameraPhotoStopRequestDTO.getPayloadIndex())
                .gateway(cameraPhotoStopRequestDTO.getGateway())
                .execute();
    }

    @Override
    public void cameraRecordingStart(CameraRecordStartRequestDTO cameraRecordStartRequestDTO) throws MqttException {
        FlyServicesExecutor.create()
                .cameraRecordStart(cameraRecordStartRequestDTO.getPayloadIndex())
                .gateway(cameraRecordStartRequestDTO.getGateway())
                .execute();
    }

    @Override
    public void cameraRecordingStop(CameraRecordStopRequestDTO cameraRecordStopRequestDTO) throws MqttException {
        FlyServicesExecutor.create()
                .cameraRecordStop(cameraRecordStopRequestDTO.getPayloadIndex())
                .gateway(cameraRecordStopRequestDTO.getGateway())
                .execute();
    }

    @Override
    public void cameraModeSwitch(CameraModeSwitchRequestDTO cameraModeSwitchRequestDTO) throws MqttException {
        System.out.println(cameraModeSwitchRequestDTO.getCameraMode());
        FlyServicesExecutor.create()
                .cameraModeSwitch(cameraModeSwitchRequestDTO.getPayloadIndex(), cameraModeSwitchRequestDTO.getCameraMode())
                .gateway(cameraModeSwitchRequestDTO.getGateway())
                .execute();
    }

    @Override
    public void videoStorageSet(VideoStorageSetRequestDTO videoStorageSetRequestDTO) throws MqttException {
        FlyServicesExecutor.create()
                .videoStorageSet(videoStorageSetRequestDTO.getVideoStorageSettings(),
                        videoStorageSetRequestDTO.getPayloadIndex())
                .gateway(videoStorageSetRequestDTO.getGateway())
                .execute();
    }

    @Override
    public void photoStorageSet(PhotoStorageSetRequestDTO photoStorageSetRequestDTO) throws MqttException {
        FlyServicesExecutor.create()
                .photoStorageSet(photoStorageSetRequestDTO.getPhotoStorageSettings(), photoStorageSetRequestDTO.getPayloadIndex())
                .gateway(photoStorageSetRequestDTO.getGateway())
                .execute();
    }

//    @Override
//    public void gimbalReset(GimbalResetRequestDTO gimbalResetRequestDTO) throws MqttException {
//        FlyServicesExecutor.create()
//                .gimbalReset(gimbalResetRequestDTO.getResetMode(), gimbalResetRequestDTO.getPayloadIndex())
//                .gateway(gimbalResetRequestDTO.getGateway())
//                .execute();
//    }
//
//    @Override
//    public void cameraFocalLengthSet(CameraFocalLengthSetRequestDTO cameraFocalLengthSetRequestDTO) throws MqttException {
//        FlyServicesExecutor.create()
//                .CameraFocalLengthSet(cameraFocalLengthSetRequestDTO.getCameraType(), cameraFocalLengthSetRequestDTO.getZoomFactor(), cameraFocalLengthSetRequestDTO.getPayloadIndex())
//                .gateway(cameraFocalLengthSetRequestDTO.getGateway())
//                .execute();
//    }


    @Override
    public void cameraScreenDrag(CameraScreenDragRequestDTO cameraFocalLengthSetRequestDTO) throws MqttException {
        FlyServicesExecutor.create()
                .CameraScreenDrag(cameraFocalLengthSetRequestDTO.getLocked(), cameraFocalLengthSetRequestDTO.getPitchSpeed(),cameraFocalLengthSetRequestDTO.getYawSpeed(), cameraFocalLengthSetRequestDTO.getPayloadIndex())
                .gateway(cameraFocalLengthSetRequestDTO.getGateway())
                .execute();
    }

    @Override
    public void cameraFrameZoom(CameraFrameZoomRequestDTO cameraFrameZoomRequestDTO) throws MqttException {
        FlyServicesExecutor.create()
                .cameraFrameZoom(cameraFrameZoomRequestDTO.getLocked(), cameraFrameZoomRequestDTO.getX(),cameraFrameZoomRequestDTO.getY(),cameraFrameZoomRequestDTO.getWidth(),cameraFrameZoomRequestDTO.getHeight(),cameraFrameZoomRequestDTO.getCameraType(), cameraFrameZoomRequestDTO.getPayloadIndex())
                .gateway(cameraFrameZoomRequestDTO.getGateway())
                .execute();
    }

    @Override
    public void cameraPointFocusAction(CameraPointFocusActionDTO cameraPointFocusAction) throws MqttException {
        FlyServicesExecutor.create()
                .cameraPointFocusAction(cameraPointFocusAction.getX(), cameraPointFocusAction.getY(),cameraPointFocusAction.getCameraType(), cameraPointFocusAction.getPayloadIndex())
                .gateway(cameraPointFocusAction.getGateway())
                .execute();
    }

    @Override
    public void cameraAim(CameraAimDTO cameraAimDTO) throws MqttException {
        FlyServicesExecutor.create()
                .cameraAim(cameraAimDTO.getX(), cameraAimDTO.getY(),cameraAimDTO.getCameraType(), cameraAimDTO.getPayloadIndex(),cameraAimDTO.getLocked())
                .gateway(cameraAimDTO.getGateway())
                .execute();
    }


}
